#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "myapp.h"
#include "mysqldb.h"
#include "cJSON.h"
#include "string.h"
#include "dblink_string.h"

int in_json_parse(void* in, size_t len, struct __in_json* injson)
{
  char _in[DUMB_INCR_PROTO_RXBUF_MAX_SIZE]; 
  memset(_in, 0, DUMB_INCR_PROTO_RXBUF_MAX_SIZE);
  
  strncpy(_in, in, len);
  printf("in ->> %s\tlen:%d(bytes)\n", _in, (int)len);
  printf("rx buf MAX length: %d(bytes)\n", DUMB_INCR_PROTO_RXBUF_MAX_SIZE);

  cJSON* json = cJSON_Parse(_in);
  
  injson->json = json;
  injson->Action = cJSON_GetObjectItem(json, "Action")->valuestring;
  injson->Data = cJSON_GetObjectItem(json, "Data");

  printf("Action ->> %s\n", injson->Action);
  
  return 0;
}

void in_json_ack(struct __in_json* injson)
{
  if(injson->json != NULL)
    cJSON_Delete(injson->json);
}

int wss_rcv_callback_2(struct lws *wsi,
                    void(*minimal_destroy_message)(void*),
                    struct vhd__dumb_increment *vhd, 
                    cJSON *json_data)
{
    /*
    if (vhd->amsg.payload)
        minimal_destroy_message(&vhd->amsg);
    */
    
    if(json_data == NULL){
        printf("Parse product information failed!\n");
        return 1;
    }
    char* Name = cJSON_GetObjectItem(json_data, "Name")->valuestring;
    double Value = atof(cJSON_GetObjectItem(json_data, "Value")->valuestring);
    int Quantity = atoi(cJSON_GetObjectItem(json_data, "Quantity")->valuestring);
    char* Factory = cJSON_GetObjectItem(json_data, "Factory")->valuestring;

    char query[256];
    sprintf(query, "insert into Product value('%s',%f,%d,'%s');", 
                                    Name, Value, Quantity, Factory);
    #if 0
    struct __mydb_conn_info conn;
      strcpy(conn.host, "172.24.0.244"); /* server hostname or IP address */ 
      strcpy(conn.user, "mysqluser01");        /* mysql user */
      strcpy(conn.pass, "12345678.com");         /* password */
      strcpy(conn.db, "db01");              /* default database to use, NULL for none */
      conn.port = 3306;           /* port number, 0 for default */
      //conn.sockpipe = NULL;        /* socket file or named pipe name */
      conn.flag = CLIENT_FOUND_ROWS; /* connection flags */

    mydb_open(&conn);
    #endif
    mydb_open(&dblinkstring);
    mydb_query(query, NULL);
    mydb_query("select * from Product;", NULL);
    mydb_exit();

    return 0;
}


struct __user_query_session *  wss_rcv_callback_3(struct lws *wsi,
                    void(*minimal_destroy_message)(void*),
                    struct vhd__dumb_increment *vhd, 
                    cJSON *json_data)
{
    struct __user_query_session *uqueryses;
    uqueryses = (struct __user_query_session*)malloc(sizeof(struct __user_query_session));
    uqueryses->string = NULL;
    uqueryses->wsi = wsi;
    uqueryses->json = cJSON_CreateArray();
    #if 0
     struct __mydb_conn_info conn;
      strcpy(conn.host, "172.24.0.244"); /* server hostname or IP address */ 
      strcpy(conn.user, "mysqluser01");        /* mysql user */
      strcpy(conn.pass, "12345678.com");         /* password */
      strcpy(conn.db, "db01");              /* default database to use, NULL for none */
      conn.port = 3306;           /* port number, 0 for default */
      //conn.sockpipe = NULL;        /* socket file or named pipe name */
      conn.flag = CLIENT_FOUND_ROWS; /* connection flags */

    mydb_open(&conn);
    #endif
    mydb_open(&dblinkstring);
    mydb_query("select * from Product;", uqueryses);
    mydb_exit();

    return uqueryses;
}

int wss_rcv_callback_3_ack(struct __user_query_session* uqueryses)
{
  uint8_t buf[LWS_PRE + DUMB_INCR_PROTO_TXBUF_MAX_SIZE]; 
  uint8_t *p = &buf[LWS_PRE];
  int n; 
  int m;
  if(uqueryses->string != NULL){
    n = lws_snprintf((char *)p, sizeof(buf) - LWS_PRE, "%s",
         uqueryses->string);
    m = lws_write(uqueryses->wsi, p, n, LWS_WRITE_TEXT);
    if (m < n) {
      lwsl_err("ERROR %d writing to di socket\n", n);
      return -1;
    }
  }

  if(uqueryses->json != NULL)
    cJSON_Delete(uqueryses->json);
  uqueryses->string = NULL;
  
  if(uqueryses != NULL)
    free(uqueryses);

  return 0;
}